package com.zhang.service;

import com.zhang.entity.DeviceInfo;
import com.zhang.entity.bo.CheckServiceStatusBody;
import com.zhang.entity.bo.DeviceStatus;
import com.zhang.handle.NettyMessageHandle;
import com.zhang.mapper.DeviceInfoMapper;
import com.zhang.util.CommandRedisUtil;
import com.zhang.util.DeviceRedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zhang.common.Constant.DEVICE_NOT_RUNNING;
import static com.zhang.common.Constant.DEVICE_RUNNING;

/**
 * User: 张
 * Date: 2024-11-14
 * Time: 9:03
 * Description:
 */
@Service
public class MonitorService {
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;
    @Autowired
    private DeviceRedisUtil deviceRedisUtil;
    @Autowired
    private CommandRedisUtil commandRedisUtil;
    /**
     * 检测设备是否在线
     * 将netty中的服务列表和数据库中的设备列表进行对比
     * @return
     */
    public CheckServiceStatusBody checkServiceStatus(){
        // todo 检查设备是否在线
        HashMap<String, List<DeviceStatus>> result = new HashMap<>();
        Map<String, String> allDevice = deviceRedisUtil.getAllDevice();
        if (allDevice == null || allDevice.isEmpty()){
            for (DeviceInfo deviceInfo : deviceInfoMapper.getAllDevice()) {
                if (!result.containsKey(deviceInfo.getServerName())){
                    ArrayList<DeviceStatus> temp = new ArrayList<>();
                    temp.add(new DeviceStatus(deviceInfo.getServerIp(), deviceInfo.getDeviceName(),false));
                    result.put(deviceInfo.getServerName(),temp);
                }else {
                    result.get(deviceInfo.getServerName()).add(new DeviceStatus(deviceInfo.getServerIp(), deviceInfo.getDeviceName(),false));
                }
            }
        }else {
            for (DeviceInfo deviceInfo : deviceInfoMapper.getAllDevice()) {
                if (!result.containsKey(deviceInfo.getServerName())){
                    ArrayList<DeviceStatus> temp = new ArrayList<>();
                    if (allDevice.containsValue(deviceInfo.getServerIp())) temp.add(new DeviceStatus(deviceInfo.getServerIp(), deviceInfo.getDeviceName(),true));
                    else temp.add(new DeviceStatus(deviceInfo.getServerIp(), deviceInfo.getDeviceName(),false));
                    result.put(deviceInfo.getServerName(),temp);
                }else {
                    if (allDevice.containsValue(deviceInfo.getServerIp())) {
                        result.get(deviceInfo.getServerName()).add(new DeviceStatus(deviceInfo.getServerIp(), deviceInfo.getDeviceName(),true));
                    }else {
                        result.get(deviceInfo.getServerName()).add(new DeviceStatus(deviceInfo.getServerIp(), deviceInfo.getDeviceName(),false));
                    }
                }
            }
        }

        // todo 获取各设备的运行状态
        Map<DeviceInfo, String> deviceStatusMap = new HashMap<>();
        Map<String, String> allCommand = commandRedisUtil.getAllCommand();
        for (DeviceInfo deviceInfo : deviceInfoMapper.getAllDevice()) {
            if (allCommand.get(deviceInfo.getServerIp()) == null) deviceStatusMap.put(deviceInfo,DEVICE_NOT_RUNNING);
            else deviceStatusMap.put(deviceInfo,DEVICE_RUNNING);
        }


        return new CheckServiceStatusBody(result,deviceStatusMap);
    }

}
